layui.use(['layer', 'form', 'admin', 'ax'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;

    // 让当前iframe弹层高度适应
    admin.iframeAuto();

    //负责人列表
    var ajax_manager = new $ax(Feng.ctxPath+"/mgr/getByRoleOfManager",function (data) {
        if(data){
            $.each(data,function(k,v){
                $("[name=managerPersonId]").append(new Option(v.name,v.userId));
            })
            form.render("select");
        }
    });
    ajax_manager.start();

    //初始化角色的详情数据
    var ajax = new $ax(Feng.ctxPath + "/bridge/view/" + Feng.getUrlParam("bridgeId"));
    var result = ajax.start();
    form.val('bridgeForm',result.data);
    form.render();




    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/bridge/edit", function (data) {
            Feng.success("修改成功!");

            //传给上个页面，刷新table用
            admin.putTempData('formOk', true);

            //关掉对话框
            admin.closeThisDialog();
        }, function (data) {
            Feng.error("修改失败!" + data.responseJSON.message + "!");
        });
        ajax.set(data.field);
        ajax.start();
    });

    var districtAjaxFunction = function(cityCode){
        new $ax(Feng.ctxPath + "/area/queryAreaByParentCode?parentCode="+cityCode, function (cityResult) {

            $.each(cityResult.data,function(k,v){
                var option = new Option(v.areaName,v.areaCode);
                $("[name=districtCode]").append(option);
            });
            form.render("select");
        }).start();
    }

    var cityAjaxFunc = function (provinceCode){
        new $ax(Feng.ctxPath + "/area/queryAreaByParentCode?parentCode="+provinceCode, function (cityResult) {
            if(cityResult.success == true){
                $.each(cityResult.data,function(k,v){
                    var option = new Option(v.areaName,v.areaCode);
                    $("[name=cityCode]").append(option);
                });
                form.render("select");
                var cityCode = cityResult.data[0]['areaCode'];
                districtAjaxFunction(cityCode);
            }
        }).start();
    }

    //省列表
    var privonceAjax =  new $ax(Feng.ctxPath + "/area/queryProvince", function (provinceResult) {
        if(provinceResult.success == true){
            $.each(provinceResult.data,function(k,v){
                var option = new Option(v.areaName,v.areaCode);
                // option.setAttribute("beamNo",v.beamNo);
                $("[name=provinceCode]").append(option);
            });
            form.render("select");
            // var provinceCode = provinceResult.data[0]['areaCode'];
            // cityAjaxFunc(provinceCode);

        }
    });
    privonceAjax.start();


    form.on('select(changeProvince)', function(data){
        $("[name=cityCode]").empty();
        $("[name=cityCode]").append(new Option("请选择市",""));
        cityAjaxFunc(data.value);
    });

    form.on('select(changecity)', function(data){
        $("[name=districtCode]").empty();
        $("[name=districtCode]").append(new Option("请选择县/区",""));
        districtAjaxFunction(data.value);
    });
});